var darkMode = getCookie("darkMode", parse = true);
let userConnectType = "";
if (darkMode){
    document.documentElement.setAttribute('theme','dark');
}else {
    document.documentElement.setAttribute('theme','light');
}
$("#userConnect").hide();
$("#welcome").text("加载中...");
$("#welcome").show();
function detectKeydown(e) {
    if (e.keyCode === 13) {
        userConnect();
    }
}
function switchUserConnect(connectType, cur_connectType) {
    document.getElementById(connectType).style.backgroundColor = "blue";
    document.getElementById(connectType).style.color = "white";
    document.getElementById(cur_connectType).style.backgroundColor = "white";
    document.getElementById(cur_connectType).style.color = "black";
    if (connectType === "login") {
        $("#check_passwd").fadeOut("fast");
    } else {
        $("#check_passwd").fadeIn("fast");
    }
    userConnectType = connectType;
}
function getUrlRelativePath() {
    var url = document.location.toString();
    var arrUrl = url.split("//");
    var start = arrUrl[1].indexOf("/");
    var relUrl = arrUrl[1].substring(start);
    if (relUrl.indexOf("?") != -1) {
        relUrl = relUrl.split("?")[0];
    }
    return relUrl;
}
function userConnect() {
    $("#wrongUsername").html("");
    $("#wrongPassword").html("");
    $("#wrongCheckPassword").html("");
    let rtn = false;
    username = String($("#getUsername").val());
    if (username.length === 0 || username.length > 30) {
        $("#wrongUsername").html("*用户名长度错误(长度1~30)");
        rtn = true;
    }
    password = base64encode(String($("#getPassword").val()));
    if (base64decode(password).length < 7 || base64decode(password).length > 30) {
        $("#wrongPassword").html("*密码长度错误(长度7~30)");
        rtn = true;
    }
    let check_password = base64encode(String($("#checkPassword").val()));
    if (userConnectType === "register" && check_password != password) {
        $("#wrongCheckPassword").html("*两次密码不一致");
        rtn = true;
    }
    if (rtn) {
        return false;
    } else {
        $("#userConnect").hide();
        $("#welcome").show();
        $("#welcome").text("等待服务器验证...");
        if ($("#ifUpdateCookie").prop("checked")) {
            setCookie("username", username, 1);
            setCookie("password", password, 1);
        }
        wsSender({ "action": "connect", "type": userConnectType, "username": username, "password": base64decode(password), "path": getUrlRelativePath() });
        return false;
    }
}
switchUserConnect("login", "register");
$(function () {//在网页加载完毕后自动执行,等同于$(document).ready()
    try { if (!username) username = getCookie("username"); }
    catch (error) { username = getCookie("username"); }
    try { if (!password) password = getCookie("password"); }
    catch (error) { password = getCookie("password"); }
    if (username && password) {
        $("#userConnect").hide();
        $("#welcome").show();
        $("#welcome").text("正在获取用户信息...");
        setCookie("username", username, 1);
        setCookie("password", password, 1);
        $("#welcome").text("正在建立连接...");
        $("#welcome").text("等待服务器验证...");
        wsSender({ "action": "connect", "type": userConnectType, "username": username, "password": base64decode(password), "path": getUrlRelativePath() });
    } else {
        $("#welcome").text("");
        $("#welcome").hide();
        $("#userConnect").fadeIn();
    }
})